package com.senthink.gl.business.auth;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.senthink.gl.business.common.converter.Converter;
import com.senthink.gl.business.domain.entity.User;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 注册用户时，接口传递的数据对象
 *
 * @author Ethan
 * @date 2017/11/16
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "注册用户时，接口传递的数据对象")
@Data
public class RegisterUserRequest {

    /**
     * 姓名
     */
    @NotBlank(message = "{user.name.notBlank}")
    @Size(min = 1, max = 20, message = "{user.name.size}")
    private String name;

    /**
     * 账号
     */
    @NotBlank(message = "{user.account.notBlank}")
    @Size(min = 1, max = 60, message = "{user.account.size}")
    private String account;

    /**
     * 手机号
     */
    @NotBlank(message = "{user.mobile.notBlank}")
    @Pattern(regexp = "^1[34578]\\d{9}$", message = "{user.mobile.pattern}")
    private String mobile;

    /**
     * 密码
     */
    @NotBlank(message = "{user.password.notBlank}")
    private String password;

    private String aid;

    /**
     * 角色
     */
    private String role;

    /**
     * 备注
     */
    private String remarks;


    public static User convertTo(RegisterUserRequest userDTO) {
        UserConverter converter = new UserConverter();
        return converter.forward(userDTO);
    }

    private static class UserConverter implements Converter<RegisterUserRequest, User> {
        @Override
        public User forward(RegisterUserRequest userDTO) {
            User user = new User();
            BeanUtils.copyProperties(userDTO, user);
            return user;
        }

        @Override
        public RegisterUserRequest backward(User user) {
            throw new AssertionError("backward is not support");
        }
    }
}
